package top.wangjingxin.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import top.wangjingxin.base.Result;
import top.wangjingxin.dao.ArticleDao;
import top.wangjingxin.model.article.*;
import top.wangjingxin.util.DeleteUtil;
import top.wangjingxin.util.ElasticUtil;
import top.wangjingxin.util.Page;
import top.wangjingxin.util.UuidUtil;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static java.lang.Integer.parseInt;
import static top.wangjingxin.cache.ResultCache.*;
import static top.wangjingxin.config.AppConfig.ROOT;

@Service
public class ArticleService {
    @Autowired
    ArticleDao articleDao;
    @Autowired
    ElasticUtil elasticUtil;

    public UploadVO upload(MultipartFile file) {
        String id = UuidUtil.Uuid();
        try {
            file.transferTo(new File(ROOT + File.separator + "resource" + File.separator + id));
        } catch (IOException e) {
            e.printStackTrace();
            return new UploadVO(file, id, "IO");
        }
        return new UploadVO(file, id, "SUCCESS");
    }

    @Transactional
    public Result get(int type, int subType, Page page) {
        int count = articleDao.count(type,subType);
        if(count==0){
            ArticleShowVO vo = new ArticleShowVO();
            vo.setCount(0);
            return getDataOk(vo);
        }
        if (count > 1) {
            return getDataOk(ArticleShowVO.get(articleDao.getList(type, subType, page.conversion()), count));
        }
        articleDao.visit(type,subType);
        return getDataOk(ArticleShowVO.get(articleDao.get(type,subType)));
    }

    @Transactional
    public Result getById(int id) {
        articleDao.visitId(id);
        return getDataOk(articleDao.getById(id));
    }

    public Result add(ArticleDTO articleDTO) {
        articleDTO.setSummary(getSummary(articleDTO.getPlainContent()));
        articleDao.add(articleDTO);
        return getDataOk(articleDTO.getId());
    }

    public Result delete(int id) {
        String s = articleDao.getContent(id);
        if (s != null) {
            Arrays.stream(s.split(",")).forEach(DeleteUtil::delete);
        }
        return getCache(articleDao.delete(id));
    }

    public Result update(ArticleDTO articleDTO) {
        articleDTO.setSummary(getSummary(articleDTO.getPlainContent()));
        return getCache(articleDao.update(articleDTO));
    }

    private String getSummary(String content) {
        if (content.length() >= 54) {
            return content.substring(0, 53) + "...";
        }
        return content + "...";
    }

    public Result search(Page page, String key) {
        SearchVO vo = elasticUtil.query(key, page.conversion());
        vo.getData().forEach(v -> {
            ArticleSimpleTO to = articleDao.getSimpleById(v.getId());
            v.setAuthor(to.getAuthor());
            v.setDate(to.getDate());
            v.setTitle(to.getTitle());
            v.setType(to.getType());
        });
        return getDataOk(vo);
    }

    public Result test() {
        //  (elasticUtil::insert);
        return OK;
    }

    public Result config() {
        return getDataOk(articleDao.config());
    }

    public Result updateName(String id, String name) {
        JSONObject object = JSONObject.parseObject(articleDao.config().getNameMap());
        object.put(id, name);
        return getDataOk(articleDao.updateName(object.toJSONString()));
    }

    public Result updateBelong(String id, String belong) {
        JSONArray array = JSONObject.parseArray(articleDao.config().getBelongMap());
        for (int i = 0; i < array.size(); i++) {
            if (array.getJSONObject(i).getIntValue("id") == parseInt(id)) {
                array.getJSONObject(i).put("child", JSONObject.parseArray(belong));
            }
        }
        return getDataOk(articleDao.updateBelong(array.toJSONString()));
    }
}
